package com.derbysoft.ota.application.util;

import com.derbysoft.ota.application.translator.CodeTables;
import com.derbysoft.ota.interfaces.protocol._2009a.POSType;
import com.derbysoft.ota.interfaces.protocol._2009a.SourceType;
import com.derbysoft.ota.interfaces.protocol._2009a.SourceType.RequestorID;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.googlecode.functionalcollections.FunctionalIterables;

public abstract class RequestorIDs {

	public static RequestorID of(POSType posType) {
		SourceType detected = FunctionalIterables.make(posType.getSource()).detect(new Predicate<SourceType>() {
			public boolean apply(SourceType input) {
				return input.getRequestorID() != null && CodeTables.UIT_TRAVEL_AGENCY.equals(input.getRequestorID().getType());
			}
		});
		Preconditions.checkNotNull(detected, "Please provide the requestor id of type [" + CodeTables.UIT_TRAVEL_AGENCY + "] in POSType.");
		return detected.getRequestorID();
	}
}
